<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Log;
use think\Queue;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Bill extends Backend
{

    /**
     * Bill模型对象
     * @var \app\common\model\Bill
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Bill;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 重新提交发票
     * @param null $ids
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function againt($ids = NULL)
    {
        Log::info($ids);
        $pk = $this->model->getPk();
        $list = $this->model->where($pk, 'in', $ids)->select();
        $count = $ignore = 0;
        Db::startTrans();
        try {
            foreach ($list as $k => $v) {
                if (in_array($v->status, [1, 2, 20, 21, 3, 31])) {
                    $ignore++;
                    continue;
                } elseif ($v->status == 22) { //实际发票状态是 开票失败
                    if ($v->client_status == 0 && $v->bill_type ==1){//客户端显示是开票中的才可以再次提交（如果已经显示开票失败了，就不能再提交了）
                        $count++;
                        Queue::push(\app\job\Bill::class, ['member_id' => $v->member_id, 'bill_id' => $v->id], 'message');
                        $v->status = 1;
                        $v->save();
                    }
                }
            }
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();

    }

    /**
     * 批量设置开票失败
     * @param null $ids
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function fail($ids = NULL)
    {
        Log::info($ids);
        $pk = $this->model->getPk();
        $list = $this->model->where($pk, 'in', $ids)->select();
        $count = $ignore = 0;
        Db::startTrans();
        try {
            foreach ($list as $k => $v) {
                if (($v->status == 22 || $v->status == 24) && $v->bill_type ==1){//实际发票状态是 开票失败
                    if ($v->client_status == 0){//客户端显示是开票中的才可以设置为开票失败
                        $v->client_status = 3;
                        $v->save();
                    }
                }
            }
            Db::commit();
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();

    }
}
